<template>
  <div class="app-container">
    <el-page-header @back="goBack" content="用户添加">
      <template #title>
        <span class="page-title">
          <i class="el-icon-user-solid" /> 用户管理
        </span>
      </template>
    </el-page-header>

    <el-card class="form-card">
      <div class="el-card__header">
        <h3 class="form-title" style="font-size: 14px; text-align: center">
          <i class="el-icon-edit" /> 新建用户信息
        </h3>
      </div>

      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="form.username" placeholder="请输入用户名" />
            </el-form-item>

            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password" placeholder="请输入密码" show-password />
            </el-form-item>

            <el-form-item label="用户角色" prop="role">
              <el-select v-model="form.role" placeholder="请选择角色" style="width: 100%">
                <el-option
                  v-for="item in roleOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item class="form-actions">
          <el-button @click="resetForm">重置</el-button>
          <el-button type="primary" @click="submitForm">立即创建</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: '',
        password: '',
        role: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '密码长度6-16位', trigger: 'blur' }
        ],
        role: [
          { required: true, message: '请选择用户角色', trigger: 'change' }
        ]
      },
      roleOptions: [
        { value: 'admin', label: '管理员' },
        { value: 'user', label: '普通用户' }
      ]
    }
  },
  methods: {
    goBack() {
      this.$router.go(-1)
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$message.success('用户创建成功')
          this.resetForm()
        }
      })
    },
    resetForm() {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style scoped>
.form-card {
  width: 600px;
  margin: 20px auto 0;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.el-card__header {
  padding: 12px 20px;
}

.el-form-item {
  margin-bottom: 18px;
}

.el-row {
  margin: 0 -10px;
}

.el-col {
  padding: 0 10px;
}

.el-card__header {
  border-bottom: 1px solid #ebeef5;
  padding: 18px 20px;
}

.form-title {
  font-size: 16px;
  color: #303133;
  display: flex;
  align-items: center;
}

.form-title i {
  margin-right: 8px;
  color: #409EFF;
}

.el-form-item {
  margin-bottom: 22px;
}

.el-form-item :deep(.el-form-item__label) {
  color: #606266;
  font-weight: 500;
}

.form-actions {
  margin-top: 30px;
  display: flex;
  justify-content: center;
  gap: 20px;
}
</style>

